<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>员工中心</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="js/jquery/jquery.min.js"></script>
    <script src="js/jquery.validation/1.14.0/jquery.validate.min.js"></script>
    <script src="js/jquery.validation/1.14.0/messages_zh.min.js"></script>
    <style>
        input.error{
            border: 1px solid #E6594E;
        }
    </style>
</head>
<body>
<div class="container">

    <form id="addEmpForm">
        <br/>
        <div class="form-group">
            <label for="ename">姓名：</label>
            <input type="text" class="form-control" id="ename" name="ename" placeholder="输入姓名">
        </div>
        <div class="form-group">
            <label for="salary">工资：</label>
            <input type="text" class="form-control" id="salary" name="salary" placeholder="输入工资">
        </div>
        <div class="form-group">
            <label for="deptid">部门编号：</label>
            <input type="text" class="form-control" id="deptid" name="deptid" placeholder="输入部门编号">
        </div>
        <div class="form-group">
            <label for="hiredate">入职日期：</label>
            <input type="text" class="form-control" id="hiredate" name="hiredate" placeholder="输入入职日期">
        </div>
        <div class="form-group">
            <button type="button" id="saveBtn" class="btn btn-success">提交</button>
            <button type="button" id="cancelBtn" class="btn btn-default">取消</button>
        </div>
    </form>
</div>

<script>
    var addEmp = function(){
        if(!check().form()){
            return;
        }
        $.ajax({
            type: "GET",
            dataType: "json",
            url: "/emp/addEmp",
            data: {
                "ename": $("#ename").val(),
                "salary":$("#salary").val(),
                "deptid":$("#deptid").val(),
                "hiredate":$("#hiredate").val(),
            },
            success: function(msg){
                //alert(msg);
                $('#cancelBtn').click();
            }
        });
    }

    $('#saveBtn').on('click', function(){
        addEmp();
    });

    $('#cancelBtn').on('click', function(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.getLikeEmp();
        parent.layer.close(index);
    });


    //校验字段是否正确
    function check(){
        /*返回一个validate对象，这个对象有一个form方法，返回的是是否通过验证*/
        return $("#addEmpForm").validate({
            rules:{
                ename:{
                    required:true
                },
                salary:{
                    required:true
                },
                deptid:{
                    required:true
                },
                hiredate:{
                    required:true
                }
            },
            messages:{
                ename:{
                    required:""
                },
                salary:{
                    required:""
                },
                deptid:{
                    required:""
                },
                hiredate:{
                    required:""
                }
            }
        });
    }

</script>
</body>
</html>